package com.yonggle.springbootdemo.controller;

import com.github.pagehelper.PageInfo;
import com.yonggle.springbootdemo.common.ResponseModel;
import com.yonggle.springbootdemo.common.ResponsePageModel;
import com.yonggle.springbootdemo.model.User;
import com.yonggle.springbootdemo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * create user
     * @param name
     * @param pswd
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseModel<Boolean> createUser(String name, String pswd, HttpServletRequest request) {
        ResponseModel<Boolean> responseModel = new ResponseModel<>();
        try {
            Boolean user = userService.createUser(name, pswd);
            responseModel.setSuccessful(true);
            responseModel.setCode("0");
            responseModel.setData(user);

        } catch (Exception e) {
            logger.error("create user error!", e);
            responseModel.setSuccessful(false);
            responseModel.setCode("99");
            responseModel.setErrorMessage("create user error:" + e.getMessage());
        }
        return responseModel;
    }

    /**
     * get user
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public ResponseModel<User> getUser(@PathVariable("userId") Long userId) {
        ResponseModel<User> responseModel = new ResponseModel<>();
        try {
            User user = userService.getUserById(userId);
            responseModel.setSuccessful(true);
            responseModel.setCode("0");
            responseModel.setData(user);

        } catch (Exception e) {
            logger.error("get user error!", e);
            responseModel.setSuccessful(false);
            responseModel.setCode("99");
            responseModel.setErrorMessage("get user error:" + e.getMessage());
        }
        return responseModel;
    }

    /**
     * list user
     * @param pageNum
     * @param pageSize
     * @param order
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponsePageModel<User> listUser(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "20") Integer pageSize,
                                            @RequestParam(defaultValue = "name") String order) {
        ResponsePageModel<User> responseModel = new ResponsePageModel<>();
        try {
            PageInfo<User> userPageInfo = userService.listUserPage(pageNum, pageSize, order);
            responseModel.setSuccessful(true);
            responseModel.setCode("0");
            responseModel.setData(userPageInfo);

        } catch (Exception e) {
            logger.error("list user error!", e);
            responseModel.setSuccessful(false);
            responseModel.setCode("99");
            responseModel.setErrorMessage("list user error:" + e.getMessage());
        }
        return responseModel;
    }
}
